import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Icon, Popconfirm } from 'antd';
import { bindActionCreators } from 'redux';
import SearchList from './SearchList/SearchList';
import { CommonTable } from 'components';
import { listNofityAction } from './index';
import styles from './ListNofity.less';
import moment from 'moment';
import { axios } from 'utils';
// const convertParams = (obj = {}) => {
//     const { time = [], count = '', type = '' } = obj;
//     const isArr = Array.isArray(time) && time.length > 0;
//     return {
//         startTime: isArr ? time[0].format('YYYY-MM-DD') : '',
//         endTime: isArr ? time[1].format('YYYY-MM-DD') : '',
//         count,
//         type
//     };
// }

class listNofity extends Component{

    constructor(props) {
        super(props);
        this.state = {};
        this.pageSize = 10;
        const _this = this;
        // this.searchParams = convertParams({});
        this.convertParams = (obj = {}) => {
            const { time = [], count = '', type = '' } = obj;
            const isArr = Array.isArray(time) && time.length > 0;
            return {
                startTime: isArr ? time[0].format('YYYY-MM-DD') : '',
                endTime: isArr ? time[1].format('YYYY-MM-DD') : '',
                count,
                type
            };
        }
        this.columns = [
            {
                title: '手机号码',
                dataIndex: 'phone',
                key: 'phone'
            },
            {
                title: '姓名',
                dataIndex: 'name',
                key: 'name'
            },
            {
                title: '身份证号',
                dataIndex: 'idcard',
                key: 'idcard'
            },
            {
                title: '渠道ID',
                dataIndex: 'channelId',
                key: 'channelId'
            },
            {
                title: '提醒次数',
                dataIndex: 'count',
                key: 'count',
            },
            {
                title: '操作',
                dataIndex: 'operatorName',
                key: 'operatorName',
                render(text, record) {
                    return (
                        <div onClick={() => _this.editTreeList(record)}>
                            <a href="javascript:;">已提醒</a>
                        </div>
                    )
                }
            }

        ];
    }
    // componentDidMount() {
    //     const { getTableData, searchParams } = this.props;
    //     let params = this.convertParams(searchParams);
    //     getTableData({ pageSize: this.pageSize, pageNum: 1, ...params });
    // }

    componentWillUnmount() {
        const { setTableData } = this.props;
        setTableData({
            data: [],
            pagination: {}
        })
    }

    handleSearch = (obj) => {
        const { setSearchParams, getTableData } = this.props;
        setSearchParams(obj)
        const params = this.convertParams(obj);
        getTableData({ pageSize: this.pageSize, pageNum: 1, ...params });
    }
    handlePageChange = (info) => {
        const { current, pageSize } = info;
        const { getTableData, searchParams } = this.props;
        const params = this.convertParams(searchParams);
        getTableData({ pageNum: current, pageSize, ...params });
    }

    // 提醒
    editTreeList = (record) => {
        const { userId, type } = record;
        const { setSearchParams, changeModalVisible} = this.props;
        axios({
            url: '/hs/admin/liushi/notify',
            method: 'post',
            data: {
                userId,
                type
            }
        }).then((a) =>{
            if( a.code == 200 ) {
                const { getTableData, searchParams } = this.props;
                const params = this.convertParams(searchParams);
                getTableData({ pageSize: this.pageSize, pageNum: 1, ...params});
            }

        })

        // setSearchParams(userId, type)
        // changeModalVisible(true);

        
    }


    render() {
        const { 
            tableData: { data, pagination },
            loading,
            searchParams
        } = this.props;
        return (
            <div>
                <SearchList 
                    submit={this.handleSearch} 
                    params={searchParams}
                />
                <CommonTable
                    columns={this.columns}
                    pagination={pagination}
                    handlePageChange={this.handlePageChange} 
                    loading={loading} 
                    dataSource={data} 
                />
            </div>
        );
    }
}
const mapStateToProps = (state) => {
    const { liushiState: { listNofityState } } = state;
    return {
        tableData: listNofityState['data'],
        loading: listNofityState['loading'],
        // 
        searchParams: listNofityState['params'],
    }
};
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: listNofityAction.lnGetTableData,
        setTableData: listNofityAction.lnSetTableData,
        // 
        setSearchParams: listNofityAction.lnChangeSearchParams,
        changeModalVisible: listNofityAction.lnChangeModalVisible,
    },dispatch)
}
export default connect(mapStateToProps, mapDispatchToProps)(listNofity);